package gindemo

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"time"
)

func v1get(c *gin.Context)  {
	/*
	fmt.Println("=======>")
	fmt.Println(c.Get("k1"))
	fmt.Println(c.Get("k2"))
	fmt.Println(c.Keys["k3"])
	*/

    fmt.Print("====v1get")
    c.JSON(200,"===>")
}
func v1Config(c *gin.Context)  {
	fmt.Println("====>")
	v.Set("name","jiba")
	v.Set("mysql",map[string]interface{}{"host":"5,5,5,5","port":1234})
	v.Set("redis",map[string]interface{}{"host":"5,5,5,5","port":1234})
	v.WriteConfig()
	c.JSON(200,configobj)
}

func QosVC(c *gin.Context)  {
        c.JSON(200,"抢到了")
}

func SigleFileUplad(c *gin.Context){

	file,_ := c.FormFile("f1")
	ssname:=fmt.Sprintf("%d-%s",time.Now().UnixNano(),file.Filename)
	c.SaveUploadedFile(file,ssname)
	fmt.Println(file.Size)
	c.JSON(200,map[string]string{"name":ssname})
}

func MulFileUplad(c *gin.Context){
	// 多文件
	form, _ := c.MultipartForm()
	files := form.File["f1[]"]
    var resultFileNames []string
	for _, file := range files {
		ssname:=fmt.Sprintf("%d-%s",time.Now().UnixNano(),file.Filename)
		c.SaveUploadedFile(file, ssname)
		fmt.Println("文件大小",file.Size) //1k = 1*1024  1m = 1*1024*1024
		resultFileNames=append(resultFileNames,ssname)
	}
	c.JSON(200,map[string][]string{"name":resultFileNames})
}


func FileDown(c *gin.Context){
	//fistmethod
	/*
	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "pngpng.png"))//fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命ss名
	c.Writer.Header().Add("Content-Type", "application/octet-stream")
	c.File("./static/1.png")
	 */
//c.Data()
	//secondMethod
	file, err := ioutil.ReadFile("./static/1.png")
	fmt.Println(file,err)
	c.JSON(200,map[string][]byte{"message":file})

}